package com.mathset.presentation.controller;

import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.mathset.bean.Question;
import com.mathset.business.service.IQuestionService;


@Controller
@RequestMapping("/question")
public class QuestionController {

	@Autowired
	private IQuestionService questionService;
	
	@RequestMapping(value="/retrieveQuestions", method=RequestMethod.POST)
	public ResponseEntity<List<Question>> retieveQuestions(@RequestParam(value="startIndex", required=true)int startIndex) {
		return new ResponseEntity<List<Question>>(questionService.retrieveQuestions(startIndex), HttpStatus.OK);
	}
	
	@RequestMapping(value="/askQuestion", method=RequestMethod.GET)
	public ModelAndView askQuestion(HttpSession session) {
		ModelAndView modelAndView =new ModelAndView("question/askQuestion");
		modelAndView.addObject(new Question());
		return modelAndView;
	}
	
	@RequestMapping(value="/submitQuestion", method=RequestMethod.POST)
	public ModelAndView submitQuestion(@Valid @ModelAttribute("question")Question question, BindingResult bindingResult) {
		ModelAndView modelAndView = new ModelAndView();
		if(bindingResult.hasErrors()) {
			modelAndView.setViewName("question/askQuestion");
		} else {
			questionService.addQuestion(question);
			modelAndView.setView(new RedirectView("redirect:viewQuestion?id=" + question.getTID()));
		}
		return modelAndView;
	}
	
	@RequestMapping(value="/modifyQuestion", method=RequestMethod.POST)
	public ModelAndView submitQuestion(@ModelAttribute("question")Question question) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("question/modifyQuestion");
		return modelAndView;
	}
	
	@RequestMapping(value="/submitModifiedQuestion", method=RequestMethod.POST)
	public ModelAndView submitModifiedQuestion(@Valid @ModelAttribute("question")Question question, BindingResult bindingResult) {
		ModelAndView modelAndView = new ModelAndView();
		if(bindingResult.hasErrors()) {
			modelAndView.setViewName("question/modifyQuestion");
		} else {
			questionService.updateQuestion(question);
			modelAndView.setViewName("question/viewQuestion");
		}
		return modelAndView;
	}
	
	@RequestMapping(value="/viewQuestion", method=RequestMethod.GET)
	public ModelAndView viewQuestion(@RequestParam(value="id",required=true)int id) {
		Question question = questionService.retrieveQuestionById(id);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("question/viewQuestion");
		modelAndView.addObject(question);
		return modelAndView;
	}
	
	public static void main(String[] args) {
//		JavaMailSenderImpl senderImpl  =   new  JavaMailSenderImpl(); 
//		    senderImpl.setHost( "smtp.163.com" );
//		    SimpleMailMessage mailMessage  =   new  SimpleMailMessage(); 
//		    
//		    mailMessage.setTo( "61645362@qq.com" ); 
//		    mailMessage.setFrom( "shucaoji@163.com " ); 
//		    mailMessage.setSubject( "测试简单文本邮件发送！ " ); 
//		    mailMessage.setText( " 测试我的简单邮件发送机制！！ " ); 
//		    senderImpl.setUsername( "shucaoji" ) ; 
//		    senderImpl.setPassword( "scj@163.com" ) ;
//		 Properties prop  =   new  Properties() ;
//		 prop.put( "mail.smtp.auth" ,  " true " ) ; 
//		 prop.put( "mail.smtp.timeout" ,  " 25000 " ) ; 
//		 senderImpl.setJavaMailProperties(prop);  
//		    senderImpl.send(mailMessage); 
//		    System.out.println( " 邮件发送成功.. " ); 
		JavaMailSenderImpl javaMailSenderImpl  =   new  JavaMailSenderImpl();   
		MimeMessage mailMessage = javaMailSenderImpl.createMimeMessage();  
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, "UTF-8");  
        try {  
            messageHelper.setTo("61645362@qq.com"); 
            messageHelper.setFrom("shucaoji@163.com");  
            messageHelper.setSubject("html"); 
            javaMailSenderImpl.setHost("smtp.163.com");
            javaMailSenderImpl.setUsername("shucaoji");
            javaMailSenderImpl.setPassword("scj@163.com");
            Properties prop  =   new  Properties() ;
            prop.put( "mail.smtp.auth" ,  " true " ) ; 
   		 	prop.put( "mail.smtp.timeout" ,  " 25000 " ) ; 
   		 	javaMailSenderImpl.setJavaMailProperties(prop); 
   		 	String VALIDATING_ADDRESS = "http://localhost:8080/user/mailValidate?token=";
            StringBuffer content = new StringBuffer("<a href=\"");
//            content.append(VALIDATING_ADDRESS);
        	content.append("1231231243254353456sdgdfsgfdsgdsfgdsfgdfg");
        	content.append("\">");
        	content.append(VALIDATING_ADDRESS);
        	content.append("1231231243254353456sdgdfsgfdsgdsfgdsfgdfg");
        	content.append("</a>");
            messageHelper.setText(content.toString(), true); 
            javaMailSenderImpl.send(mailMessage); 
        } catch (MessagingException e) {
            throw new RuntimeException("failed to send mail");  
        }  
	}
}
